﻿using habbo.Cryptography;
using habbo.Habbo.Characters;
using habbo.Kernel.Packets.Messages;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;

namespace habbo.Kernel.Network
{
    public class Session
    {
        /// <summary>
        /// Integer to number this class.
        /// </summary>
        private int Id { get; set; }

        public Socket Socket
        {
            get;
            set;
        }

        public Protocol HabboEncryption
        {
            get;
            set;
        }

        public ProgressType SessionProgress
        {
            get;
            set;
        }

        public SocketAsyncEventArgs ReceiveEventArgs
        {
            get;
            set;
        }

        public int SendBytesRemainingCount
        {
            get;
            set;
        }

        public Character Character
        {
            get;
            set;
        }

        public int BytesSentAlreadyCount
        {
            get;
            set;
        }

        public byte[] DataToSend
        {
            get;
            set;
        }

        public void OnConnectionClose()
        {
            SystemApp.ConsoleSystem.PrintLine("GLOBAL", "Session was removed.");
        }

        /// <summary>
        /// Sets everything you need.
        /// </summary>
        /// <param name="Id"></param>
        /// <param name="Socket"></param>
        /// <param name="EventArgs"></param>
        public void Update(int Id, Socket Socket, SocketAsyncEventArgs EventArgs)
        {
            this.Id = Id;
            this.Socket = Socket;
            this.ReceiveEventArgs = EventArgs;
            this.SessionProgress = ProgressType.Connecting;
        }

        /// <summary>
        /// Updates the session-progess.
        /// </summary>
        /// <param name="SessionProgress"></param>
        public void UpdateSessionProgress(ProgressType SessionProgress)
        {
            this.SessionProgress = SessionProgress;
        }

        /// <summary>
        /// Updates the character.
        /// </summary>
        /// <param name="Character"></param>
        public void UpdateCharacter(Character Character)
        {
            this.Character = Character;
            this.UpdateSessionProgress(ProgressType.Authenticated);
            this.Character.OnLogin();
        }

        public void SendAlert(string Message)
        {
            ServerPacket alert = new ServerPacket(2313);
            alert.Append(Message);
            alert.Append(string.Empty);
            this.Send(alert);
        }

        public void setId(int playerID)
        {
            this.Id = playerID;
        }

        public void SendAlert(string Message, string URL)
        {
            ServerPacket alert = new ServerPacket(2313);
            alert.Append(Message);
            alert.Append(URL);
            this.Send(alert);
        }

        public void Send(byte[] Data)
        {
            Socket.Send(Data);
        }

        public void Send(ServerPacket Packet)
        {
            Socket.Send(Packet.ToByteArray());
        }

        public void FinishSend(IAsyncResult Result)
        {
            if (!Result.IsCompleted)
            {
                // > FAIL
            }
        }

        public Character getCharacter()
        {
            return Character;
        }
    }

    public enum ProgressType
    {
        OnStack,
        Connecting,
        PolicyRequest,
        Authenticated
    }
}
